// import axios from 'axios';
import instance from './request'

// 定义基础URL，与后端接口地址对应
// const baseURL = 'http://172.20.10.10:3000/';
// 后续可使用 baseURL 来简化请求地址，避免重复书写


// 获取部门数据
export const getDepartments = () => {
  return instance.get('http://172.20.10.10:3000/department');
};


// 添加组织
export const addDepartment = (data: any) => {
  return instance.post('http://172.20.10.10:3000/department', data);
};


// 删除部门（需用户添加）
export const deleteDepartment = (id: any) => {
  return instance.delete(`http://172.20.10.10:3000/department/${id}`); // 替换为实际后端接口地址
};


// 编辑部门（新增）
export const updateDepartment = (id: any, data: any) => {
  return instance.put(`http://172.20.10.10:3000/department/${id}`, data);
};


// 按部门负责人查询部门（新增）
export const getDepartmentsByDepartmentHead = (departmentHead: any) => {
  return instance.get(`http://172.20.10.10:3000/department?head=${encodeURIComponent(departmentHead)}`);
};






const baseURL = 'http://172.20.10.10:3000';

export interface addFormInlineTS {
  username: string,
  phone: string,
  id: string,
  department: string,
  employeestype: string,
  password: string
}
const token = localStorage.getItem('token')

// 获取所有用户
export const getAllUsers = () => {
  return instance.get(`http://172.20.10.10:3000/user`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按姓名查询用户
export const queryUserByUsername = (username: string) => {
  return instance.get(`http://172.20.10.10:3000/user?username=${username}`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按手机号查询用户
export const queryUserByPhone = (phone: string) => {
  return instance.get(`http://172.20.10.10:3000/user?phone=${phone}`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 按部门查询用户
export const queryUserByDepartment = (department: string) => {
  return instance.get(`http://172.20.10.10:3000/user?department=${department}`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};
//删除接口
export const deleteUser = (id: string) => {
  return instance.delete(`http://172.20.10.10:3000/user/${id}`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};


// 按多条件组合查询（示例：同时传姓名和部门）
export const queryUserByMultiConditions = ({ username, phone, department, status }: { username: string, phone: string, department: string, status: string }) => {
  const obj = {
    username,
    phone,
    department
  }
  const params = new URLSearchParams(obj).toString();
  return instance.get(`http://172.20.10.10:3000/user?${params}`, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    },
  });
};

// 添加用户
export const addTable = (userData: any) => {
  return instance.post(`http://172.20.10.10:3000/user`, userData, {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    }
  });
};

export const updateuser = (id: any, data: any) => {
  return instance.put(`http://172.20.10.10:3000/user/${id}`, data, {
    headers: {
      Authorization: `Bearer ${token}`, // 使用已获取的Token
    }
  });
};

// ================= 岗位管理相关接口 =================

// 获取岗位列表（支持分页和条件查询）
export const getJobList = (params: { jobName?: string; jobCode?: string; jobType?: string;}) => {
  return instance.get('http://172.20.10.10:3000/jobManagement', { params });
};

// 添加岗位
export const addJob = (data: any) => {
  return instance.post('http://172.20.10.10:3000/jobManagement', data);
};

// 根据岗位名称查询岗位（可复用 getJobList，单独导出便于调用）
export const queryJobByName = (jobName: string) => {
  return instance.get('http://172.20.10.10:3000/jobManagement', { params: { jobName } });
};

// 删除岗位
export const deleteJob = (id: string) => {
  return instance.delete(`http://172.20.10.10:3000/jobManagement/${id}`);
};

// 编辑岗位
export const updateJob = (id: string, data: any) => {
  return instance.put(`http://172.20.10.10:3000/jobManagement/${id}`, data);
};





